﻿using System.Windows;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        /// <summary>
        /// 在当前网格子面板左侧或右侧插入一列。
        /// </summary>
        /// <param name="atLeft">插入位置。默认为左侧。</param>
        public static bool InsertColumn(bool atLeft = true, bool withoutErrorMessage = false)
        {
            if (Globals.selectedEditor == null) return false;
            if (Globals.selectedEditor.SelectedExpression == null)
            {
                if (withoutErrorMessage == false)
                {
                    MessageBox.Show("请先选中一个网格子面板，然后再执行此一操作。",
                            Globals.appName,
                            MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                return false;//只有网格式子面板或其子级公式才能进行操作。
            }

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            ModifingItem mi = new ModifingItem() { OldSelectedExID = curEx.ExID };

            if ((curEx is Expressions.GridPanel) == false)
            {
                if (curEx.ParentPanel != null && curEx.ParentPanel is Expressions.GridPanel)
                {
                    curEx = curEx.ParentPanel;
                }
                else
                {
                    if (withoutErrorMessage == false)
                    {
                        MessageBox.Show("请先选中一个网格子面板，然后再执行此一操作。",
                            Globals.appName,
                            MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                    return false;//只有网格式子面板或其子级公式才能进行操作。
                }
            }

            Expressions.GridPanel gp = curEx as Expressions.GridPanel;
            Expressions.Grid grid = gp.ParentExpression as Expressions.Grid;
            string oldXmlText = grid.XmlData.OuterXml;

            grid.InsertColumn(gp, mi, atLeft);

            //Action act = new Action(grid.ExID, grid.ParentPanel.BasePanel.Children.IndexOf(grid),
            //            grid.GetType().Name, "OutXmlText", oldXmlText, grid.XmlData.OuterXml);

            //mi.Add(act);
            grid.Editor.Do(mi);
            return true;
        }
    }
}
